package com.zjp.attendance.service;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.zjp.attendance.domain.UserInfo;
import com.zjp.attendance.domain.cmd.*;
import com.zjp.attendance.enums.CmdCode;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.CharsetUtil;

import java.util.ArrayList;
import java.util.List;

public class CmdService {


    public String getTransId() {
        return IdUtil.nanoId();
    }

    /**
     * 获取设备基本信息
     */
    public DefaultFullHttpResponse getDeviceInfo() {
        return response(CmdCode.GET_DEVICE_INFO);
    }

    /**
     * 获得注册人员列表
     */
    public DefaultFullHttpResponse getUserIdList() {
        return response(CmdCode.GET_USER_ID_LIST);
    }

    /**
     * 设置用户信息
     *
     * @param name
     * @param userId
     * @return
     */
    public DefaultFullHttpResponse setUserInfo(String name, String userId) {
        SetUserInfoCmd userInfoCmd = new SetUserInfoCmd();
        List<UserInfo> userInfoList = new ArrayList<>();
        UserInfo info = new UserInfo();
        info.setName(name);
        info.setUserId(userId);
        userInfoList.add(info);
        userInfoCmd.setUsers(userInfoList);
        return setUserInfo(userInfoCmd);
    }

    public DefaultFullHttpResponse setUserInfo(SetUserInfoCmd userInfoCmd) {
        ByteBuf byteBuf = toByteBuf(userInfoCmd);
        return response(CmdCode.SET_USER_INFO, byteBuf);
    }

    /**
     * 从考勤机获取用户登记信息
     */
    public DefaultFullHttpResponse getUserInfo(List<String> userIds) {
        GetUserInfoCmd getUserInfoCmd = new GetUserInfoCmd();
        getUserInfoCmd.setUsersId(userIds);
        ByteBuf byteBuf = toByteBuf(getUserInfoCmd);
        return response(CmdCode.GET_USER_INFO, byteBuf);
    }

    /**
     * 删除人员
     *
     * @param userIds
     * @return
     */
    public DefaultFullHttpResponse deleteUser(List<String> userIds) {
        DeleteUserCmd deleteUserCmd = new DeleteUserCmd();
        deleteUserCmd.setUsersId(userIds);
        deleteUserCmd.setUsersCount(userIds.size());
        ByteBuf byteBuf = toByteBuf(deleteUserCmd);
        return response(CmdCode.DELETE_USER, byteBuf);

    }

    /**
     * 获取记录数据
     *
     * @param beginTime
     * @param endTime
     */
    public DefaultFullHttpResponse getLogData(String beginTime, String endTime) {
        GetLogDataCmd getLogDataCmd = new GetLogDataCmd(beginTime, endTime);
        ByteBuf byteBuf = toByteBuf(getLogDataCmd);
        return response(CmdCode.GET_LOG_DATA, byteBuf);
    }

    /**
     * 获取记录数据
     */
    public DefaultFullHttpResponse getLogData(String time) {
        return getLogData(time, time);
    }

    /**
     * 删除所有记录数据
     *
     * @return
     */
    public DefaultFullHttpResponse clearLogData() {
        return response(CmdCode.CLEAR_LOG_DATA);
    }


    /**
     * 同步时间
     *
     * @param syncTime 同步的时间 （yyyyMMddHHmmss）
     */
    public DefaultFullHttpResponse setTime(String syncTime) {
        SetTimeCmd setTimeCmd = new SetTimeCmd(syncTime);

        ByteBuf byteBuf = toByteBuf(setTimeCmd);

        return response(CmdCode.SET_TIME, byteBuf);
    }

    public ByteBuf toByteBuf(Object o) {
        String jsonStr = JSONUtil.toJsonStr(o);
        System.out.println("jsonStr.length() = " + jsonStr.length());
        System.out.println(jsonStr);
        ByteBuf byteBuf = Unpooled.copiedBuffer(jsonStr, CharsetUtil.UTF_8);
        return byteBuf;
    }


    public DefaultFullHttpResponse errorNoCmd() {
        return response(CmdCode.ERROR_NO_COM);
    }

    /**
     * 机器重新启动
     */
    public DefaultFullHttpResponse resetFK() {
        return response(CmdCode.RESET_FK);
    }

    /**
     * 清除设备管理员
     */
    public DefaultFullHttpResponse clearManager() {
        return response(CmdCode.CLEAR_MANAGER);
    }

    /**
     * 获取设备参数
     */
    public DefaultFullHttpResponse getDeviceSetting() {
        return response(CmdCode.GET_DEVICE_SETTING);
    }

    /**
     * 恢复出厂设置
     */
    public DefaultFullHttpResponse resetDevice() {
        return response(CmdCode.RESET_DEVICE);
    }

    /**
     * 重置实时登记数据标志
     */
    public DefaultFullHttpResponse resetEnrollMark() {
        return response(CmdCode.RESET_ENROLL_MARK);
    }

    /**
     * 重置实时打卡数据标志
     */
    public DefaultFullHttpResponse resetLogMark() {
        return response(CmdCode.RESET_LOG_MARK);
    }


    private DefaultFullHttpResponse response(CmdCode resetDevice) {
        DefaultFullHttpResponse defaultFullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK);
        return response(defaultFullHttpResponse, resetDevice);
    }

    private DefaultFullHttpResponse response(CmdCode resetDevice, ByteBuf byteBuf) {
        DefaultFullHttpResponse defaultFullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK, byteBuf);
        return response(defaultFullHttpResponse, resetDevice);
    }

    private DefaultFullHttpResponse response(DefaultFullHttpResponse defaultFullHttpResponse, CmdCode resetDevice) {
        setHeaders(resetDevice, defaultFullHttpResponse);
        return defaultFullHttpResponse;
    }

    public DefaultFullHttpResponse responseTrans(String transId) {
        DefaultFullHttpResponse defaultFullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK);
        defaultFullHttpResponse.headers().set("response_code", CmdCode.OK.getCode());
        defaultFullHttpResponse.headers().set("trans_id", transId);
        return defaultFullHttpResponse;
    }

    private void setHeaders(CmdCode resetDevice, DefaultFullHttpResponse defaultFullHttpResponse) {
        defaultFullHttpResponse.headers().set("response_code", CmdCode.OK.getCode());
        defaultFullHttpResponse.headers().set("cmd_code", resetDevice.getCode());
        defaultFullHttpResponse.headers().set("trans_id", getTransId());
    }


}
